--Universidade Federal de Campina Grande
--Centro de Enegenharia Elétrica e Informática
--Unidade Acadêmica de Sistemas e Computação
--Disciplina: Paradigmas de Linguagens de Programação
--Professor: Franklin Ramalho
--Equipe: Arthur Ribeiro
--        Felipe Vieira
--        Jeysibel Dantas
--        Paulo Victor
--Euphoric Checkers
--

sequence white_initial_lines = {6,7,8}
sequence black_initial_lines = {1,2,3}



--return a new Board with side size equals to 8
export function initializeBoard()
        sequence board = generateField()
        return initializePieces(board)
end function

--generate an empty 8x8  board
export function generateField()
        return repeat(repeat(0,8),8)
end function

--populate the pieces on the board
export function initializePieces(sequence board)
        for i=1 to length(white_initial_lines) do
          if (remainder(white_initial_lines[i],2) = 0) then board[white_initial_lines[i]] = putPiecesOddPosition(board[white_initial_lines[i]],"white")
          else board[white_initial_lines[i]] = putPiecesEvenPosition(board[white_initial_lines[i]],"white")
          end if
        end for

        for i=1 to length(black_initial_lines) do
          if (remainder(black_initial_lines[i],2) = 0) then board[black_initial_lines[i]] = putPiecesOddPosition(board[black_initial_lines[i]],"black")
          else board[black_initial_lines[i]] = putPiecesEvenPosition(board[black_initial_lines[i]],"black")
          end if
        end for
        return board
end function

export function putPiecesOddPosition(sequence line, sequence color)
        for i=1 to 8 by 2 do        
            if equal(color,"white") then line[i] = WHITE  
            else line[i] = BLACK
        end if  
        end for
return line     
end function

export function putPiecesEvenPosition(sequence line, sequence color)
        for i=2 to 8 by 2 do
            if equal(color,"white") then line[i] = WHITE
            else line[i] = BLACK
        end if
        end for
return line
end function

export function verifyBlackPieceExistence(integer line, integer cell)
        return board[line][cell]=2 or board[line][cell]=4
end function

export function verifyWhitePieceExistence(integer line, integer cell)
        return board[line][cell]=1 or board[line][cell]=3
end function

export function isValidPosition(integer line, integer cell)
        return line>=1 and line<=8 and cell>=1 and cell<=8
end function

--return a sequence in this order LF,RF,LB,RB
export function findWhitePieceAdjacents(atom lineStart, atom columnStart)
        return {{lineStart-1,columnStart -1}, {lineStart-1,columnStart+1}, {lineStart+1, columnStart-1}, {lineStart+1, columnStart+1}}
end function

--return a sequence in this order LF,RF,LB,RB
export function findBlackPieceAdjacents(atom lineStart, atom columnStart)
        return {{lineStart+1,columnStart+1}, {lineStart+1,columnStart-1}, {lineStart-1, columnStart+1}, {lineStart-1, columnStart-1}}
end function

export function getPossiblesAdjacents(integer line, integer cell)
        return {{line-1,cell+1}, {line-1,cell-1}, {line+1, cell+1}, {line+1, cell-1}}
end function

export procedure verifyAndCreateWhiteKing(sequence board)
    for i=1 to length(board[BLACK_ORIGIN]) do
        if board[BLACK_ORIGIN][i]=1 then
            board[BLACK_ORIGIN][i]= WHITE_KING
        end if
    end for
end procedure
        
export procedure verifyAndCreateBlackKing(sequence board)
    for i=1 to length(board[WHITE_ORIGIN]) do
        if board[WHITE_ORIGIN][i]=1 then
            board[WHITE_ORIGIN][i]= BLACK_KING
        end if
    end for
end procedure
        

export function theresOnlyKings()
    for i=1 to length(board) do
        for j=1 to length(board[i]) do
            if board[i][j]=1 or board[i][j]=2 then
                return FALSE
            end if
        end for
    end for
    
    return TRUE
end function
